/**
 *  Stream Recorder: Records RealPlayer(tm) streams as MP3s
 *  Copyright (C) 2007  David Griffiths
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package dg.radio2ipod;

import dg.inx.Editor;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ResourceBundle;
import dg.radio2ipod.model.Settings;
import dg.radio2ipod.model.Stream;
import dg.radio2ipod.view.AboutBox;
import dg.radio2ipod.view.MainFrame;
import javax.swing.UIManager;

/**
 * The central application class.
 * @author davidg
 */
public final class Main {
    static {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
    }

    private static MainFrame mainFrame;
    private static AboutBox aboutBox = new AboutBox();
    /**
     * Internationalization strings.
     */
    private static ResourceBundle resBundle = ResourceBundle.getBundle(
            "dg/radio2ipod/resource/strings");

    private Main() {
        // Prevent external instantiation
    }

    /**
     * Main method.
     * @param args the command line arguments
     */
    public static void main(final String[] args) {
        if (isMac()) {
            try {
                Class.forName("dg.radio2ipod.controller.MacAppListener");
            } catch (ClassNotFoundException cnfe) {
                cnfe.printStackTrace();
            }
        }
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        mainFrame = new MainFrame();
        getMainFrame().addWindowListener(new WindowAdapter() {
            public void windowClosing(final WindowEvent e) {
                handleQuit();
                System.exit(0);
            }
        });
        getMainFrame().setVisible(true);
        getMainFrame().saveNext();
    }

    /**
     * Add a stream to the application.
     * @param stream stream object that needs to be added to the list
     */
    public static void addStream(final Stream stream) {
        getMainFrame().addStream(stream);
    }

    /**
     * True if we are running on a Mac.
     * @return true if the current platform is a Mac, false otherwise
     */
    public static boolean isMac() {
        String osName = System.getProperty("os.name");
        return ((osName != null) && (osName.indexOf("Mac") != -1));
    }

    /**
     * Display the about box.
     */
    public static void showAbout() {
        aboutBox.setVisible(true);
    }

    /**
     * Display the preferences dialog.
     */
    public static void showPreferences() {
        Editor editor = new Editor(getMainFrame(), resBundle.getString("app.name")
        + " "
                + resBundle.getString("Preferences"), Settings.getInstance(),
                "/dg/radio2ipod/view/preferencesEditor.xml");
        editor.setVisible(true);
    }

    /**
     * Called when the application being closed down.
     */
    public static void handleQuit() {
        Stream.stopAllRecording();
        Rectangle bounds = getMainFrame().getBounds();
        Settings.getInstance().setWindowLeft(bounds.x);
        Settings.getInstance().setWindowTop(bounds.y);
        Settings.getInstance().setWindowWidth(bounds.width);
        Settings.getInstance().setWindowHeight(bounds.height);
    }

    public static MainFrame getMainFrame() {
        return mainFrame;
    }
}
